/**
 * 文章分类弹窗模块
 */
define( [
	'jquery',
	'require',
	'artCategory',
	'message',
	'../../lib/editData'
], function( $, require, artCategory ) {

	var ShowTitleMessage = require( 'message' );
	var editData = require( '../../lib/editData' );
	var category = artCategory.CATEGORY_NAME;
	var showTime = ShowTitleMessage.showTime;

	var self = $( '#category-update' );

	var CategoryEdit = {
		options: {
			categouryIsLoad: false
		},
		fullAllCategory: function() {

			function categoryHtmlCreate( categoryKey ) {
				return '<div class="cate-tag-btnOrDiv" category-ID="' + categoryKey + '">' + category[ categoryKey ] + '</div>';
			}

			for ( var key in category ) {
				$( '.category-detail', self ).append( categoryHtmlCreate( key ) );
			}
			return this;
		},
		groupOptionCreate: function( id, val ) {
			return '<option selected="selected" value="' + id + '">' + val + '</option>';
		},
		addTagName: function( val ) {
			var $this = this;
			if ( /^\s*$/.test( val ) ) {
				ShowTitleMessage.showWarning( '不能添加空的分组名称！', showTime );
			} else {
				if ( !$modalThis.options.usersGroupArray ) {
					ShowTitleMessage.showWarning( '请先等待您的分组加载完成', showTime );
					return;
				}
				$( "#doAdd-tagName", self ).prop( 'disabled', 'disabled' );
				$.ajax( {
					url: '/edit_text/doAdd_tagName',
					type: 'post',
					dataType: 'json',
					data: {
						'category_name': val
					},
					success: function( data ) {
						if ( data.error ) {
							ShowTitleMessage.showWarning( data.msg, showTime );
							return;
						}
						if ( data.success ) {
							$( "#txt-tagName-value", self ).val( '' );
							$( '#category-diy-select', self ).append( $this.groupOptionCreate( data.id, data.category_name ) ).selectpicker( 'refresh' );;
							$modalThis.options.usersGroupArray.categorys[ $modalThis.options.usersGroupArray.categorys.length + 1 ] = {
								'category_name': val
							};
							$( '.my-group', '#util_article_choose' ).append( '<button type="button" groupid="' + data.id + '">' + val + '</button>' );
							if ( $modalThis.options.usersGroupArray ) {
								var $myResource = $( '#util_article_choose .my-resource' );

								function showmore() {
									if ( $( '.my-group', $myResource ).height() > 31 ) {
										$( '.more', $myResource ).show();
									}
								}
								$( '#util_article_choose' ).show();
								if ( $myResource.css( 'display' ) === 'none' ) {
									$myResource.show();
									showmore();
									$myResource.hide();
								} else {
									showmore();
								}
								$( '#util_article_choose' ).hide();
							}
						} else {
							ShowTitleMessage.showWarning( '未知的错误:-(', showTime );
						}
					},
					complete: function() {
						$( "#doAdd-tagName", self ).removeAttr( 'disabled' );
					}
				} );
			}
		},
		onModalShow: function() {
			var $this = this;
			$( '.category-diy-select', self ).selectpicker( {
				'size': 8
			} );

			function optionCreate( optionID, optionName ) {
				return '<option value="' + optionID + '">' + optionName + '</option>';
			}
			self.on( 'show.bs.modal', function() {

				/*if (!$this.options.categouryIsLoad) {
					$modalThis.usersGroupArrayRequest(function(data) {
						$this.options.categouryIsLoad = true;
						var userGroup = data.categorys;
						for (key in userGroup) {
							$('#category-diy-select', self).append(optionCreate(userGroup[key].id, userGroup[key].category_name));
						}
						$('#category-diy-select>[value="' + editData.getAttrbute('article_category') + '"]', self).attr('selected', 'selected')
						$('#category-diy-select').selectpicker('refresh');
					});
				}*/
				if ( editData.getAttrbute( 'article_field' ) ) {
					$( '.category-detail>[category-id="' + editData.getAttrbute( 'article_field' ) + '"]', self ).trigger( 'click' );
				} else {
					$( '.category-detail>.cate-tag-btnOrDiv.selected', self ).removeClass( 'selected' );
				}
			} );
		}
	};
	// “添加分组”按钮
	self.on( 'click', '.add-group', function() {
		$( this ).hide();
		$( '.add-tagname-div', self ).show( 'normal' );
	} ).on( 'click', "#doAdd-tagName", function() {
		// 添加分组名称
		CategoryEdit.addTagName( $.trim( $( "#txt-tagName-value", self ).val() ) );
	} ).on( 'click', '.cate-tag-btnOrDiv', function() {
		// 选择分类
		$( this ).addClass( "selected" ).siblings().removeClass( "selected" );
	} ).on( 'click', '#doupdate-category', function() {
		// 分类修改确认
		var $categoryID = $( '.category-detail>.selected', self ).attr( 'category-ID' );
		var $group = $( '#category-diy-select', self ).val();
		var categoryName = category[ $categoryID ] || '';
		if ( $categoryID ) {
			$( "#cate-text-div" ).text( categoryName );
			var declareOriginal = editData.getAttrbute( "declare_original" );
			if ( declareOriginal == 1 || declareOriginal == 2 ) {
				$( '#article-category' ).text( categoryName );
			}
		}
		var customGrouName = $( '#category-diy-select [value="' + $group + '"]' ).text();
		$( '#cateDiy-text-div' ).text( customGrouName );

		editData.setAttrbute( {
			'article_category_name': customGrouName,
			'article_category': $group
		} );

		if ( $categoryID ) {
			// 数据编辑
			editData.setAttrbute( "article_field", $categoryID )
		}

		editData.setAttrbute( "article_category", $group );
		self.modal( 'hide' );

	} );
	CategoryEdit.fullAllCategory().onModalShow();
} );